package com.itboye.jinpuyiyuan.activity.login;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2017/12/19 0019.
 * 找回密码
 */

public class FindPasswordActivity extends BaseActivity implements Observer {
    //title

    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar,include_layout;

    private EditText etVertifyCode,etPhone,etResetPassword,etVertifyPassword;
    private TextView sendVerifyCode,tvSure;
    private LinearLayout phoneLl;
    private BranchAddressPresenter presenter,presenter1;
    private String phoneNumber;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_findpassword);
        include_layout.setBackgroundColor(Color.TRANSPARENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        initView();

    }


    private void initView() {
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("找回密码");
        presenter= new BranchAddressPresenter(this);
        presenter1= new BranchAddressPresenter(this);
        phoneNumber=getIntent().getStringExtra("phoneNumber");
        etPhone.setText(phoneNumber);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){

            case R.id.rlLeft:
            case R.id.leftImg:

                this.finish();
                break;
            case R.id.phoneLl:
                MAlert.alert("");
                break;
                //发送验证码
            case R.id.sendVerifyCode:
                    if (etPhone.getText().length()>20||etPhone.getText().length()<6){
                        MAlert.alert("请输入电5-20位号码");
                        return;
                    }
                    //code_type 验证码类型(1: 注册 2: 更新密码 3: 新绑定手机号 5: 登录)[必须]
                    presenter1.getVerifyCode(etPhone.getText().toString(),2,6,3,120);

                break;

                //确定
            case R.id.tvSure:

                if (etPhone.getText().toString()==null||etPhone.getText().toString().equals("")
                        ||etVertifyCode.getText().toString().equals("")||etResetPassword.getText().toString().equals("")
                        ||etResetPassword.getText().toString()==null||etVertifyPassword.getText().toString().equals("")
                        ||etVertifyPassword.getText().toString()==null){
                    MAlert.alert("请填写完整信息");
                }else if (etPhone.getText().length()>20||etPhone.getText().length()<5){
                    MAlert.alert("请输入5-20位的手机号码");
                }else if (!etResetPassword.getText().toString().equals(etVertifyPassword.getText().toString())){
                    MAlert.alert("两次密码不一致");
                }else {
                    phoneNumber=etPhone.getText().toString();
                    presenter.forgetPassword(phoneNumber,etVertifyCode.getText().toString(),etResetPassword.getText().toString());
                }
                break;


        }
    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity resultEntity = handlerError(arg);
        if (resultEntity!=null){


            if (resultEntity.getEventType()==BranchAddressPresenter.By_UserAccount_updatePwdByMobile_success){

                SPUtils.put(FindPasswordActivity.this,null, Const.LOGIN_PASSWORD,etResetPassword.getText().toString());
//                MAlert.alert("密码修改成功");
                FindPasswordActivity.this.finish();
            }else if (resultEntity.getEventType()==BranchAddressPresenter.By_UserAccount_updatePwdByMobile_fail){

//                 MAlert.alert("密码修改失败"+resultEntity.getMsg());
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_SecurityCode_create_success){
                String data = (String) resultEntity.getData();
                etVertifyCode.setText(data);
//                MAlert.alert("验证码获取成功");
            }else if (resultEntity.getEventType()== BranchAddressPresenter.By_SecurityCode_create_fail){
//                MAlert.alert("验证码获取失败");
            }
        }
    }
}
